package routes

import (
	"github.com/dlclark/regexp2"
	"github.com/go-playground/validator/v10"
	"github.com/sirupsen/logrus"
)

func EmailValid(fl validator.FieldLevel) bool {
	email := fl.Field().Interface().(string)
	if email == "" {
		return true
	}
	re, err := regexp2.Compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", regexp2.None)
	if err != nil {
		logrus.Errorf("EmailValid fail: %s", err.Error())
		return false
	}
	match, err := re.MatchString(email)
	if err != nil {
		logrus.Errorf("EmailValid fail: %s", err.Error())
		return false
	}
	return match
}

func getValidators() (validators map[string]validator.Func) {
	return map[string]validator.Func{
		"email": EmailValid,
	}
}
